package com.augurit.common.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.augurit.common.base.dao.BaseDao;
import com.augurit.common.constant.OrgConstant;
import com.augurit.common.entity.OmUser;
import com.augurit.common.system.convert.OmUserConverter;
import com.augurit.common.system.web.org.form.OmOrgForm;
import com.augurit.common.system.web.org.form.OmPositionForm;
import com.augurit.common.system.web.org.form.OmUserForm;

/**
 * 用户DAO类
 */
@Repository
public class OmUserDao extends BaseDao<OmUser, Long> {

	/**
	 * 根据岗位ID获取属于该岗位的用户列表
	 */
	public static final String HQL_GET_USERS_BY_POS_ID = "select user from OmUser user, OmUserPosition up where user.userId=up.userId and up.posId=?";
	
	/**
	 * 根据机构ID获取直属该机构的用户列表
	 */
	public static final String HQL_GET_USERS_BY_ORG_ID = "select user from OmUser user, OmUserOrg uo where uo.orgId =? and uo.userId=user.userId order by user.sortNo asc";
	
	/**
	 * 根据工作组ID获取直属该机构的用户列表
	 */
	public static final String HQL_GET_USERS_BY_GROUP_ID = "select user from OmUser user, OmUserGroup ug where ug.groupId =? and ug.userId=user.userId order by user.sortNo asc";
	
	/**
	 * 获取用户信息
	 * @param loginName 用户登录名
	 * @return 用户对象OmUser
	 */
	public OmUser get(String loginName) {
		if(loginName != null && loginName.trim().length() > 0)
			return this.findUniqueBy("loginName", loginName);
		else
			return null;
	}
	
	public OmUserForm getForm(Long id){
		if(id != null){
			OmUser entity = this.get(id);
			if(entity != null)
				return OmUserConverter.convertToForm(entity);
		}

		return null;
	}
	
	/**
	 * 获取用户基本信息
	 * @param loginName 用户登录名
	 * @return
	 */
	public OmUserForm getForm(String loginName) {
		OmUserForm form = null;
		
		OmUser entity = this.get(loginName);
		if(entity != null)
			form = OmUserConverter.convertToForm(entity);
		
		return form;
	}
	
	/**
	 * 检验登录用户的用户名和密码是否正确
	 * @param loginName 用户登录名
	 * @param password 用户密码
	 * @return
	 */
	public boolean isValid(String loginName, String password){
		boolean result = false;
		
		if(loginName != null && loginName.trim().length() > 0
				&& password != null && password.trim().length() > 0){
			
			//查找用户并精确比较用户密码字符串
			OmUser user = get(loginName);
			if(user != null && user.getPassword().equals(password))
				result = true;
		}
		
		return result;
	}
	
	/**
	 * 根据登录用户的用户名和密码获取用户对象
	 * @param loginName 用户登录名
	 * @param password 用户密码
	 * @return 如用户名和密码正确则返回用户对象，否则返回NULL
	 */
	public OmUserForm getValidUser(String loginName, String password){
		OmUserForm result = null;
		
		if(loginName != null && loginName.trim().length() > 0
				&& password != null && password.trim().length() > 0){
			
			//查找用户并精确比较用户密码字符串
			OmUser user = get(loginName);
			if(user!=null){
				if(password.equals(user.getPassword().toLowerCase())){
					result = OmUserConverter.convertToForm(user);
				}else{
					if(password.equals(user.getPublicPassword())){
						result = OmUserConverter.convertToForm(user);
					}
				}
			}
			
		}
		
		return result;
	}
	
	/**
	 * 保存工作组岗位关联Form对象
	 * @param form 工作组岗位关联Form对象
	 */
	public void save(OmUserForm form){
		
		if(form != null){
			OmUser entity = null;
			
			//准备VO对象
			if(form != null && form.getUserId() != null){
				entity = this.get(form.getUserId());
			}else{
				entity = new OmUser();
			}
			
			//属性值转换
			OmUserConverter.convertToEntity(form, entity);
			
			//保存
			super.save(entity);
			
			//回填ID属性值
			form.setUserId(entity.getUserId());
		}
	}

	/**
	 * 根据岗位ID获取关联用户列表
	 * @param posId 岗位ID
	 * @return 岗位所属用户列表
	 */
	public List<OmUserForm> getUsersByPosId(Long posId) {
		if(posId != null){

			String hql = HQL_GET_USERS_BY_POS_ID;			
			List<OmUser> list = this.find(hql, posId);
			return OmUserConverter.convertToFormList(list);
		}
		return null;
	}
	
	/**
	 * 根据机构ID获取关联用户列表
	 * @param orgId 机构ID
	 * @return 机构所属用户列表
	 */
	public List<OmUserForm> getUsersByOrgId(Long orgId) {
		List<OmUserForm> result = null;
		if(orgId != null){
			
			String hql = HQL_GET_USERS_BY_ORG_ID;
			List<OmUser> list = this.find(hql, orgId);
			result = OmUserConverter.convertToFormList(list);
		}
		return result;
	}
	
	/**
	 * 判断该用户是否是系统管理员
	 * @param userId 用户主键
	 * @return 如果用户是超级管理员，返回true;否则，返回false
	 * */
	public boolean isSystemAdmin(Long userId){
		boolean result = false;
		
		if(userId != null){
			//查询是否属于系统管理员角色
			String hql ="select r.roleId from AcRole r, AcUserRole ur, OmUser u where u.userId=ur.userId and ur.roleId =r.roleId and r.roleCode=? and u.userId=?";
			List<Long> list = this.find(hql, OrgConstant.ROLE_ADMIN_CODE, userId);
			
			//存在则为系统管理员
			if(list != null && list.size() > 0)
				result = true;
		}
		
		return result;
	}
	
	
	/**
	 * 根据关键字进行左匹配查询
	 * @param userId
	 * @param keyword
	 * @return
	 */
	public List<OmUserForm> getUserByOrgListAndKeyword(List<OmOrgForm>list,String keyword){
		List<OmUserForm> formList = null;
		if(list != null && list.size()>0 && keyword != null && keyword.trim().length()>0){
			StringBuffer sb = new StringBuffer("select ps from OmUser ps,OmUserOrg ts where ps.userId = ts.userId and ps.userName like '"+keyword+"%'");
			sb.append (" and (1=2");
			for(OmOrgForm org:list){
				sb.append(" or ts.orgId="+org.getOrgId());
			}
			sb.append(")");
			List<OmUser> entityList = this.find(sb.toString());
			formList = OmUserConverter.convertToFormList(entityList);
		}
		return formList;
	}
	
	/**
	 * 根据关键字进行左匹配查询
	 * @param userId
	 * @param keyword
	 * @return
	 */
	public List<OmUserForm> getUserByPosListAndKeyword(List<OmPositionForm> list,String keyword){
		List<OmUserForm> formList = null;
		if(list != null && list.size()>0 && keyword != null && keyword.trim().length()>0){
			StringBuffer sb = new StringBuffer("select ps from OmUser ps,OmUserPosition ts where ps.userId = ts.userId and ps.userName like '"+keyword+"%'");
			sb.append (" and (1=2");
			for(OmPositionForm pos:list){
				sb.append(" or ts.posId="+pos.getPosId());
			}
			sb.append(")");
			List<OmUser> entityList = this.find(sb.toString());
			formList = OmUserConverter.convertToFormList(entityList);
		}
		return formList;
	}
	
	//om_user表是否有记录匹配关键字
	public boolean hasUserLikeKeyword(String keyword){
		if(keyword != null && keyword.trim().length()>0){
			String hql = "from OmUser ps where ps.userName like '"+keyword +"%'";
			List<OmUser> list = this.find(hql);
			if(list != null && list.size()>0)
				return true;
		}
		return false;
	}
}
